package com.sofly.controller.group1;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.AuthorizationException;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sofly.entity.DataGrid;
import com.sofly.entity.User;
import com.sofly.service.group1.IUserService;

@Controller
public class UserController{
	
	@Autowired
	private IUserService userService; 
	
	@Autowired
	private DataGrid dataGrid;
	
	@RequestMapping("/to/{address}")
	public String toAssginPage(@PathVariable("address") String address){
		System.out.println("ing...");
		return address;
	}
	

	
	@ExceptionHandler(value={AuthorizationException.class})
	public String handleUnauthorizedException(){
		System.out.println("权限异常");
		return "redirect:/unauthorized.jsp";
	}
	
	
	@RequestMapping("/testShiroAnnotation")
	public String testShiroAnnotation(HttpSession session){
		session.setAttribute("key", "value12345");
		userService.testMethod();
		return "redirect:/list.jsp";
	}

	@RequestMapping("/login")
	public String login(@RequestParam("name") String username, 
			@RequestParam("password") String password){
		Subject currentUser = SecurityUtils.getSubject();
		if (!currentUser.isAuthenticated()) {
        	// 把用户名和密码封装为 UsernamePasswordToken 对象
            UsernamePasswordToken token = new UsernamePasswordToken(username, password);
            // remember me
            token.setRememberMe(true);
            try {
            	// 执行登录. 
                currentUser.login(token);
            } 
            // ... catch more exceptions here (maybe custom ones specific to your application?
            // 所有认证时异常的父类. 
            catch (AuthenticationException ae) {
                //unexpected condition?  error?
            	System.out.println("登录失败: " + ae.getMessage());
            }
        }
		
		return "redirect:/main.jsp";
	}
	
	@ResponseBody
	@RequestMapping("/href/adduser")
	public int addUser(User user){
		return userService.addUser(user);
	}
	
	@ResponseBody
	@RequestMapping("/href/queryuser")
	public DataGrid queryUser(){
		dataGrid.setTotal(20);
		dataGrid.setRows(userService.getAllUser());
		return dataGrid;
	}
	
	@ResponseBody
	@RequestMapping(value="/href/deleteuser",produces="text/json;charset=utf-8")
	public String deleteUser(String id){
		int userId = Integer.parseInt(id);
		int i = userService.deleteUser(userId);
		if(i==1){
			return "删除成功";
		}
		return "删除失败";
	}
	
	@ResponseBody
	@RequestMapping("/href/checkusername")
	public String checkusername(String name){
	 
		List<String> list = userService.getNameList();
		for (int i = 0; i < list.size(); i++) {
			if(list.get(i).equals(name)){
				return "error";
			}
		}
		return "success";
	}
	
}
